package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.mapper.AddressMapper;
import com.itheima.pojo.Address;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.AddressMapper;
import com.itheima.pojo.Address;
import com.itheima.pojo.CheckItem;

import com.itheima.mapper.AddressMapper;
import com.itheima.pojo.Address;

import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author aili
 * @create 2020-05-16 12:48
 */


@Service(interfaceClass = AddressService.class)
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override

    public List<Address> getAllAddress() {

        return addressMapper.getAllAddress();
    }

    public Address getAddressById(Integer id) {
        return addressMapper.getAddressById(id);
    }





    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.使用分页助手进行分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.调用checkItemMapper的findByCondition方法
        String queryString = queryPageBean.getQueryString();
        Page<Address> page = addressMapper.findByCondition(queryString);
        //3.封装放回对象
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    public void add(Address address) {
        Address addressByName = addressMapper.getAddressByName(address.getName());
        if (addressByName==null){
            addressMapper.add(address);
        }else {
            throw new RuntimeException("添加失败");
        }

    }

    @Override
    public void delete(int id) {
        addressMapper.delete(id);
    }


	    @Override
    public List<String> findAddress() {
        List<String> address = addressMapper.findAddress();
        return address;
    }
}
